﻿; (function ($) {
    var methods = {
        init: function (options) {
            var $this = $(this);
            var setting = {
                SaveEvent: null,
                IsMulti: false,
                zindex: -1,
                IsImage:true
            };
            setting = $.extend(setting, options);
            $(this).click(function () {
                parent.layer.open({
                    type: 1,
                    title: '<span></span>',
                    content: '<div class="layui-content"  style="width: calc(100vw );height: calc(100vh - 95px);"><iframe class="J_iframe" style="width: calc(100vw );height: calc(98vh);"  {0} name="editFromTempiframe" id="editFromTempiframe" frameborder="0"></iframe></div><div class="layui-footer"><button type="button" class="btn btn-primary save">确定</button> <button type="button" class="btn btn-default" id="butclose">关闭</button></div>'.replace("{0}", " src='../Manage/AttachmentImg?IsMulti=" + (setting.IsMulti ? "1" : "0") + "&IsImage=" + (setting.IsImage ? "1" : "0") + "'"),
                    anim: 'up',
                    success: function (layero, index) {
                        var editdialog = $(layero);
                        console.log(editdialog.prop("outerHTML"));
                        if (setting.zindex != -1) {
                            editdialog.css('z-index', setting.zindex);
                        }
                        editdialog.find(".save").click(function () {
                            var selectchecklist = editdialog.find("#editFromTempiframe").contents().find(".selectchecked");
                            if (selectchecklist.length == 0) {
                                ShowError("请选择对应图片！");
                                return;
                            }
                            var arrydata = [];
                            for (var idex = 0; idex < selectchecklist.length; idex++) {
                                arrydata.push({ url: $(selectchecklist[idex]).attr("url"), name: $(selectchecklist[idex]).attr("filename") })
                            }
                            if (setting.SaveEvent != null && typeof (setting.SaveEvent) == "function") {
                                setting.SaveEvent(arrydata);
                            }
                            parent.layer.close(editdialog.attr("Index"));
                        });
                        editdialog.find("#butclose").click(function () {
                            parent.layer.close(editdialog.attr("Index"));
                        });
                    }
                });
            });

        }
    }

    $.fn.SelectImage = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").SelectImage) == "function") {
            if (arguments.length > 1) {

                return window.parent.$(this).SelectImage(method, arguments[1]);
            } else {
                return window.parent.$(this).SelectImage(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };

})($);